//
//  HeartAnimation.m
//  linktuIOS
//
//  Created by sandy on 2017/1/6.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HeartAnimation.h"
 // 将CGPoint转行为比例点

CGPoint MyCGPointMake(CGFloat x, CGFloat y){
    CGFloat xValue = (x/375 * [UIScreen mainScreen].bounds.size.width);
    CGFloat yValue = (y/667 * [UIScreen mainScreen].bounds.size.height);
    return CGPointMake(xValue, yValue);
}



@interface HeartAnimation()

@property (nonatomic ,strong)NSArray * animationGroup;
@property (nonatomic,strong) NSMutableArray *pathArrayanimations;//路径动画数组
@property (nonatomic,strong) CABasicAnimation *alphAnimation;
@property (nonatomic,strong) CABasicAnimation *resizeAnimation;


@property (nonatomic ,strong)NSMutableArray * testmy;



@end


@implementation HeartAnimation



+(instancetype)singleHeart
{
    static HeartAnimation * instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[HeartAnimation alloc]init];
    });
    return instance;
}


-(NSArray *)getAnimationArray
{
    
    self.animationGroup = @[self.pathArrayanimations[arc4random()%4],self.alphAnimation,self.resizeAnimation];
    return self.animationGroup;
}


#pragma mark -- 用户点亮的动画


-(CABasicAnimation *)alphAnimation
{
    if(_alphAnimation == nil)
    {
        _alphAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        _alphAnimation.fromValue = @(1);
        _alphAnimation.toValue = @(0);
    }
    
    return _alphAnimation;
}
-(CABasicAnimation *)resizeAnimation
{
    if(_resizeAnimation == nil)
    {
        _resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
        [_resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(30, 30)]];
        _resizeAnimation.fillMode = kCAFillModeForwards;
        _resizeAnimation.removedOnCompletion = NO;
        _resizeAnimation.duration = 0.2;
        
    }
    return _resizeAnimation;
    
}

-(NSMutableArray *)pathArrayanimations
{
    {
        NSMutableArray *temparray = [NSMutableArray array];
        for(int i = 0;i < 4 ; i++)
        {
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            animation.path = [self.pathArray[i] CGPath];
            [temparray addObject:animation];
        }
        _pathArrayanimations = temparray;
        
    }
    return _pathArrayanimations;
    
}


-(NSArray *)pathArray
{
    if(_pathArray == nil)
    {
        UIBezierPath *bezierPath1 = [UIBezierPath bezierPath];
        [bezierPath1 moveToPoint: MyCGPointMake(312.5, 620.5)];
        [bezierPath1 addCurveToPoint: MyCGPointMake(323.5, 511.5) controlPoint1: MyCGPointMake(329.5, 558.5) controlPoint2: MyCGPointMake(323.5, 512.5)];
        [bezierPath1 addCurveToPoint: MyCGPointMake(341.5, 408.5) controlPoint1: MyCGPointMake(323.5, 510.5) controlPoint2: MyCGPointMake(341.5, 451.5)];
        [bezierPath1 addCurveToPoint: MyCGPointMake(323.5, 337.5) controlPoint1: MyCGPointMake(341.5, 365.5) controlPoint2: MyCGPointMake(323.5, 337.5)];
        
        
        UIBezierPath *bezierPath2 = [UIBezierPath bezierPath];
        [bezierPath2 moveToPoint: MyCGPointMake(316.5, 614.5)];
        [bezierPath2 addCurveToPoint: MyCGPointMake(352.5, 433.5) controlPoint1: MyCGPointMake(321.5, 503.5) controlPoint2: MyCGPointMake(352.5, 461.5)];
        [bezierPath2 addCurveToPoint: MyCGPointMake(352.5, 310.5) controlPoint1: MyCGPointMake(352.5, 405.5) controlPoint2: MyCGPointMake(371.5, 310.5)];
        
        
        UIBezierPath *bezierPath3 = [UIBezierPath bezierPath];
        [bezierPath3 moveToPoint: MyCGPointMake(319.5, 616.5)];
        [bezierPath3 addCurveToPoint: MyCGPointMake(319.5, 438.5) controlPoint1: MyCGPointMake(283.5, 501.5) controlPoint2: MyCGPointMake(319.5, 438.5)];
        [bezierPath3 addCurveToPoint: MyCGPointMake(334.5, 330.5) controlPoint1: MyCGPointMake(319.5, 438.5) controlPoint2: MyCGPointMake(334.5, 401.5)];
        
        UIBezierPath* bezierPath4 = UIBezierPath.bezierPath;
        [bezierPath4 moveToPoint: MyCGPointMake(316.5, 620.5)];
        [bezierPath4 addCurveToPoint: MyCGPointMake(342.5, 499.5) controlPoint1: MyCGPointMake(322.5, 610.5) controlPoint2: MyCGPointMake(342.5, 581.5)];
        [bezierPath4 addCurveToPoint: MyCGPointMake(316.5, 418.5) controlPoint1: MyCGPointMake(342.5, 417.5) controlPoint2: MyCGPointMake(316.5, 418.5)];
        [bezierPath4 addCurveToPoint: MyCGPointMake(302.5, 318.5) controlPoint1: MyCGPointMake(316.5, 418.5) controlPoint2: MyCGPointMake(288.5, 390.5)];
        
        _pathArray = @[bezierPath1,bezierPath2,bezierPath3,bezierPath4];
        
    }
    return _pathArray;
}
@end
